<?php
/*
* Author：鑫宇工作室
* QQ：639519801
* url：www.xinyuream.com
*/
include_once $_SERVER['DOCUMENT_ROOT'].'/func/include.php';
$user=admin_check(true);
?>

<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
						<div class="layui-inline">
                            <div class="layui-input-inline">
                                <input type="text" name="ip" placeholder="请输入IP" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
							<div class="layui-input-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 查 询</button>
							</div>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="addordel">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加IP </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 批量删除 </button>
            </div>
        </script>
        <table class="layui-hide" id="datalist" lay-filter="datalist"></table>
        <script type="text/html" id="caozuo">
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>
    </div>
</div>

<script>
    layui.use(['form', 'table','miniPage','element'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniPage = layui.miniPage;
		form.render();
        table.render({
			method:'post',
            elem: '#datalist',
            url: '/admin?ac=waf',
            toolbar: '#addordel',
			cellMinWidth:180,
            defaultToolbar: [],
			response: {statusCode:200},
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id',width: 80, align: "center", title: 'ID', sort: true},
                {field: 'ip', align: "center",  title: '封锁IP'},
                {field: 'stime', align: "center",  title: '封锁时间'},
                {field: 'rule', align: "center",  title: '命中规则'},
                {title: '操作', align: "center",width:150,  toolbar: '#caozuo'}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: <?=intval(config('limit'))?>,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            table.reload('datalist', {
                page: {
                    curr: 1
                }
                , where: data.field
            }, 'data');

            return false;
        });
		
		
		
        /**
         * toolbar事件监听
         */
		
        table.on('toolbar(datalist)', function (obj) {
            if (obj.event === 'add') {   // 监听添加操作
                var content = miniPage.getHrefContent('manage/addwaf');
                var openWH = miniPage.getOpenWidthHeight();
                var index = layer.open({
                    title: '添加IP',
                    type: 1,
                    shade: 0.2,
                    area:['30%','auto'],
                    maxmin:true,
                    shadeClose: true,
                    content: content,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
				
            } else if (obj.event === 'delete') {  // 监听删除操作
                var data = table.checkStatus('datalist');
				if(data.data.length<1){
					layer.msg('未选择IP',{icon: 5,time: 2000});
					return false;
				}
				var ids=$.map(data.data,function(d){return d.id;}).join('|');
				layer.confirm('确认要删除IP吗', function (index) {
					$.post({
						url:'/admin?ac=delwaf',
						data: {"ids" :ids},
						type:'post',
						dataType:'json',
						success:function(e){
							layui.exit(e);
							if(e.code===200){
								layer.msg(e.msg,{icon: 6,time: 2000});
								 table.reload('datalist');
							}else{
								layer.msg(e.msg,{icon: 5,time: 2000});
							}
						},
						error: function(e){
						  layer.msg('数据异常，操作失败！',{icon: 5,time: 2000});
						}
					});
                    layer.close(index);
                });
            }
        });
        table.on('tool(datalist)', function (obj) {
           if (obj.event === 'delete') {
                layer.confirm('确认要删除此IP', function (index) {
					$.post({
						url:'/admin?ac=delwaf',
						data: {"ids" :obj.data.id},
						type:'post',
						dataType:'json',
						success:function(e){
							layui.exit(e);
							if(e.code===200){
								layer.msg(e.msg,{icon: 6,time: 2000});
								 table.reload('datalist');
							}else{
								layer.msg(e.msg,{icon: 5,time: 2000});
							}
						},
						error: function(e){
						  layer.msg('数据异常，操作失败！',{icon: 5,time: 2000});
						}
					});
                    layer.close(index);
                });
            }
        });
    });
</script>